# Maintainer: Antoni Aloy <aaloytorrens@gmail.com>
pkgname=codeblocks
pkgver=20.03
pkgrel=2
pkgdesc="Cross-platform C/C++ and Fortran IDE"
url="https://codeblocks.org"
# riscv64: ftbfs
arch="all !riscv64"
license="GPL-3.0-or-later"
# coreutils is needed because ./bootstrap calls a rm parameter that is not supported in BusyBox
makedepends="
	libtool automake autoconf coreutils imagemagick zip
	boost-dev hunspell-dev wxgtk3-dev tinyxml-dev
	"
subpackages="$pkgname-dev $pkgname-doc $pkgname-contrib"
source="https://sourceforge.net/projects/codeblocks/files/Sources/$pkgver/codeblocks-$pkgver.tar.xz
	0001-v20.03-r12008.patch
	0002-v20.03-r12012.patch
	0003-fix-missing-sentinel.patch
	0004-delete-wxpropgrid.patch
	0005-fix-pkgconfig-codeblocks-version.patch
	0006-fix-openfileslistplugin-gcc11.patch
	"
options="!check" # No test suite

prepare() {
	default_prepare

	# wxpropgrid fails building. Remove the directory and patch files
	# Upstream has deleted it due to unmanteinance
	rm -vr src/sdk/wxpropgrid
	convert src/mime/codeblocks.png +set date:create +set date:modify -background none -extent 64x64 src/mime/codeblocks.png
	sed -i 's|$(datadir)/pixmaps|$(datadir)/icons/hicolor/64x64/apps|' src/mime/Makefile.am
	sed -i 's|$(datarootdir)/appdata|$(datarootdir)/metainfo|' Makefile.am src/plugins/contrib/appdata/Makefile.am
	./bootstrap

	# Patch auto_revision's Makefile.am to use the host compiler and flags
	cd src/build_tools/autorevision
	sed -i Makefile.am \
		-e "/noinst_PROGRAMS = auto_revision/a CC       = gcc" \
		-e "/noinst_PROGRAMS = auto_revision/a CXX      = g++" \
		-e "/noinst_PROGRAMS = auto_revision/a LD       = g++" \
		-e "/noinst_PROGRAMS = auto_revision/a CFLAGS   = -O2 -pipe" \
		-e "/noinst_PROGRAMS = auto_revision/a CXXFLAGS = -O2 -pipe" \
		-e "/noinst_PROGRAMS = auto_revision/a LDFLAGS  = -s"
}

build() {
	# Deactivate wxsmith-related plugins because they depend on wxpropgrid
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--with-boost-libdir=/usr/lib \
		--with-contrib-plugins=all,-FileManager,-wxsmith,-wxsmithcontrib,-wxsmithaui \
		--with-wx-config=/usr/bin/wx-config-gtk3 \
		--prefix=/usr
	sed -i 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
	make
}

package() {
	make DESTDIR="$pkgdir" install

	# Replace pkgconfig file for 20.03 version (Reason: 20.03svn version is invalid)
	find "$pkgdir" -type f -exec sed -i 's/20.03svn/20.03/g' {} +
}

dev() {
	default_dev
	pkgdesc="$pkgdesc (SDK)"
}

contrib() {
	pkgdesc="$pkgdesc (contrib plugins)"
	mkdir -p "$subpkgdir"/usr/share/metainfo
	mkdir -p "$subpkgdir"/usr/share/codeblocks/images

	# Move plugins and wxContribItems to this subpackage
	amove usr/lib/codeblocks

	# Move contrib metainfo file
	# While at it, also place it in the correct directory
	mv -v "$pkgdir"/usr/share/metainfo/codeblocks-contrib.metainfo.xml \
	"$subpkgdir"/usr/share/metainfo/codeblocks-contrib.metainfo.xml

	# Move optional plugins to this subcategory
	mv -v "$pkgdir"/usr/share/codeblocks/AutoVersioning.zip \
	"$subpkgdir"/usr/share/codeblocks/AutoVersioning.zip

	mv -v "$pkgdir"/usr/share/codeblocks/BrowseTracker.zip \
	"$subpkgdir"/usr/share/codeblocks/BrowseTracker.zip

	mv -v "$pkgdir"/usr/share/codeblocks/Cccc.zip \
	"$subpkgdir"/usr/share/codeblocks/Cccc.zip

	mv -v "$pkgdir"/usr/share/codeblocks/CppCheck.zip \
	"$subpkgdir"/usr/share/codeblocks/CppCheck.zip

	mv -v "$pkgdir"/usr/share/codeblocks/Cscope.zip \
	"$subpkgdir"/usr/share/codeblocks/Cscope.zip

	mv -v "$pkgdir"/usr/share/codeblocks/DoxyBlocks.zip \
	"$subpkgdir"/usr/share/codeblocks/DoxyBlocks.zip

	mv -v "$pkgdir"/usr/share/codeblocks/EditorConfig.zip \
	"$subpkgdir"/usr/share/codeblocks/EditorConfig.zip

	mv -v "$pkgdir"/usr/share/codeblocks/EditorTweaks.zip \
	"$subpkgdir"/usr/share/codeblocks/EditorTweaks.zip

	mv -v "$pkgdir"/usr/share/codeblocks/HexEditor.zip \
	"$subpkgdir"/usr/share/codeblocks/HexEditor.zip

	mv -v "$pkgdir"/usr/share/codeblocks/IncrementalSearch.zip \
	"$subpkgdir"/usr/share/codeblocks/IncrementalSearch.zip

	mv -v "$pkgdir"/usr/share/codeblocks/MouseSap.zip \
	"$subpkgdir"/usr/share/codeblocks/MouseSap.zip

	mv -v "$pkgdir"/usr/share/codeblocks/NassiShneiderman.zip \
	"$subpkgdir"/usr/share/codeblocks/NassiShneiderman.zip

	mv -v "$pkgdir"/usr/share/codeblocks/Profiler.zip \
	"$subpkgdir"/usr/share/codeblocks/Profiler.zip

	mv -v "$pkgdir"/usr/share/codeblocks/ProjectOptionsManipulator.zip \
	"$subpkgdir"/usr/share/codeblocks/ProjectOptionsManipulator.zip

	mv -v "$pkgdir"/usr/share/codeblocks/RegExTestbed.zip \
	"$subpkgdir"/usr/share/codeblocks/RegExTestbed.zip

	mv -v "$pkgdir"/usr/share/codeblocks/ReopenEditor.zip \
	"$subpkgdir"/usr/share/codeblocks/ReopenEditor.zip

	mv -v "$pkgdir"/usr/share/codeblocks/SmartIndentCpp.zip \
	"$subpkgdir"/usr/share/codeblocks/SmartIndentCpp.zip

	mv -v "$pkgdir"/usr/share/codeblocks/SmartIndentFortran.zip \
	"$subpkgdir"/usr/share/codeblocks/SmartIndentFortran.zip

	mv -v "$pkgdir"/usr/share/codeblocks/SmartIndentHDL.zip \
	"$subpkgdir"/usr/share/codeblocks/SmartIndentHDL.zip

	mv -v "$pkgdir"/usr/share/codeblocks/SmartIndentLua.zip \
	"$subpkgdir"/usr/share/codeblocks/SmartIndentLua.zip

	mv -v "$pkgdir"/usr/share/codeblocks/SmartIndentPascal.zip \
	"$subpkgdir"/usr/share/codeblocks/SmartIndentPascal.zip

	mv -v "$pkgdir"/usr/share/codeblocks/SmartIndentPython.zip \
	"$subpkgdir"/usr/share/codeblocks/SmartIndentPython.zip

	mv -v "$pkgdir"/usr/share/codeblocks/SmartIndentXML.zip \
	"$subpkgdir"/usr/share/codeblocks/SmartIndentXML.zip

	mv -v "$pkgdir"/usr/share/codeblocks/SpellChecker \
	"$subpkgdir"/usr/share/codeblocks/SpellChecker

	mv -v "$pkgdir"/usr/share/codeblocks/SpellChecker.zip \
	"$subpkgdir"/usr/share/codeblocks/SpellChecker.zip

	mv -v "$pkgdir"/usr/share/codeblocks/SymTab.zip \
	"$subpkgdir"/usr/share/codeblocks/SymTab.zip

	mv -v "$pkgdir"/usr/share/codeblocks/ThreadSearch.zip \
	"$subpkgdir"/usr/share/codeblocks/ThreadSearch.zip

	mv -v "$pkgdir"/usr/share/codeblocks/ToolsPlus.zip \
	"$subpkgdir"/usr/share/codeblocks/ToolsPlus.zip

	mv -v "$pkgdir"/usr/share/codeblocks/Valgrind.zip \
	"$subpkgdir"/usr/share/codeblocks/Valgrind.zip

	mv -v "$pkgdir"/usr/share/codeblocks/abbreviations.zip \
	"$subpkgdir"/usr/share/codeblocks/abbreviations.zip

	mv -v "$pkgdir"/usr/share/codeblocks/byogames.zip \
	"$subpkgdir"/usr/share/codeblocks/byogames.zip

	mv -v "$pkgdir"/usr/share/codeblocks/cb_koders.zip \
	"$subpkgdir"/usr/share/codeblocks/cb_koders.zip

	mv -v "$pkgdir"/usr/share/codeblocks/codesnippets.zip \
	"$subpkgdir"/usr/share/codeblocks/codesnippets.zip

	mv -v "$pkgdir"/usr/share/codeblocks/codestat.zip \
	"$subpkgdir"/usr/share/codeblocks/codestat.zip

	mv -v "$pkgdir"/usr/share/codeblocks/copystrings.zip \
	"$subpkgdir"/usr/share/codeblocks/copystrings.zip

	mv -v "$pkgdir"/usr/share/codeblocks/dragscroll.zip \
	"$subpkgdir"/usr/share/codeblocks/dragscroll.zip

	mv -v "$pkgdir"/usr/share/codeblocks/envvars.zip \
	"$subpkgdir"/usr/share/codeblocks/envvars.zip

	mv -v "$pkgdir"/usr/share/codeblocks/exporter.zip \
	"$subpkgdir"/usr/share/codeblocks/exporter.zip

	mv -v "$pkgdir"/usr/share/codeblocks/headerfixup.zip \
	"$subpkgdir"/usr/share/codeblocks/headerfixup.zip

	mv -v "$pkgdir"/usr/share/codeblocks/help_plugin.zip \
	"$subpkgdir"/usr/share/codeblocks/help_plugin.zip

	mv -v "$pkgdir"/usr/share/codeblocks/images/codesnippets \
	"$subpkgdir"/usr/share/codeblocks/images/codesnippets

	mv -v "$pkgdir"/usr/share/codeblocks/keybinder.zip \
	"$subpkgdir"/usr/share/codeblocks/keybinder.zip

	mv -v "$pkgdir"/usr/share/codeblocks/lib_finder \
	"$subpkgdir"/usr/share/codeblocks/lib_finder

	mv -v "$pkgdir"/usr/share/codeblocks/lib_finder.zip \
	"$subpkgdir"/usr/share/codeblocks/lib_finder.zip

	mv -v "$pkgdir"/usr/share/codeblocks/occurrenceshighlighting.zip \
	"$subpkgdir"/usr/share/codeblocks/occurrenceshighlighting.zip

	mv -v "$pkgdir"/usr/share/codeblocks/rndgen.zip \
	"$subpkgdir"/usr/share/codeblocks/rndgen.zip
}

sha512sums="
f97c1a02d31f0be4dc08b1f19b57bd0d3cba375ab5f33ab91b2a2df7817018f5b3e3063ed07199a3970644f1caa7c7e511db2034c73264f5f4963046d65b0097  codeblocks-20.03.tar.xz
98584a93790974481a669cb86a01efe42690fe4a4e0a6c04abc15e3ea0c037be0b0cdc63164907e0c9f51414bb32b2879f6a06117d1374a4288a0a03bec4080f  0001-v20.03-r12008.patch
1580f63e559470f58990831c49c8885ba980416326bc66a5564a47681d9f33124809824d7e6caab2ebfbde8bf57def4e9e06a41d30379bb7aa5a2deaee3803c1  0002-v20.03-r12012.patch
d8ef0d5fd0605b00d72833591df78b6160bd4d3ae02523b0eba796bc6e375384b241bb24c3a44b45df7a47bf47bf58f1f94eb8459d177ab516383617955e0dd0  0003-fix-missing-sentinel.patch
1b642ad9e4c479fb81674c4fa412e3c6d169bad9cad4b6748433004e5e0c31f1d1aba9a81eb02eb6ffd8927ae96d56a8fd90f009b7aa0925ae2e397794fedbac  0004-delete-wxpropgrid.patch
792fcb9b570e5ee0fbadc2ab68b17f3f76c667b6b0822b6e4a89ce5e40c2afcd8744bbaa65fb3252fc36d9343d7c439df3f759c4fbc9980fcf851f5cad6be178  0005-fix-pkgconfig-codeblocks-version.patch
56e649ae3cde529fdc9e6b11eac652c692b381bdb803cc476f536b28904f724cb0446974a3bed2d95740dff7ad9d026d1987800aef1f462a02ee1d81c6bd0318  0006-fix-openfileslistplugin-gcc11.patch
"
